import React from 'react';

export default class Clock extends React.Component {
  timerID = null;
  constructor(props) {
    super(props);
    this.state = {
      date: new Date(),
    };
  }

  componentDidMount() {
    // 组件被挂载上去的时候会被调用
    // 开启定时器
    this.timerID = setInterval(this.tick, 1000);
  }

  componentWillUnmount() {
    //  组件被卸载时会被调用
    // 清除定时器
    clearInterval(this.timerID);
  }

  tick = () => {
    this.setState({
      date: new Date(),
    });
  };

  handleClick(param1, e) {
    console.log('param1 :>> ', param1);
    console.log('e :>> ', e);
  }

  render() {
    return (
      <div>
        <h1>Hello world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
        <button onClick={this.handleClick.bind(this, 1)}>click me</button>
      </div>
    );
  }
}
